<?php
/**
 * 正常 返回一个分润数组
 * 错误 返回false
 */
require_once 'streamconfig.php';
class Scenicstream extends Streamconfig
{
    //订单分润表别名
    protected $goods_profit;
    protected $bargaining;
    protected $user;
    protected $user_account;

    //需要返回的参数
    public $orderPayTotal;    //议价时的订单支付。
    public $calculate_method; //是否1:未议价 2:议价
    public $supply_price = 0; //供应价
    public $shop_price = 0;   //销售价

    //分润必要参数
    protected $sellerUid; //购买者uid
    protected $bitTrade;  //购买者用户的类型
    protected $parent_id; //上级用户编号id
    protected $cat_id;    //门票类型
    protected $goodsId;   //门票id
    protected $bitWan;    //1:在线支付；2：到付
    protected $number;    //当前购买数量

    public function __construct($tables = array())
    {
        parent::__construct();
        if (count($tables) > 0) {
            $this->initialize($tables);
        }
    }

    /**
     * 初始化数据
     * @param unknown $props
     */
    public function initialize($tables)
    {
        foreach ($tables as $alias=>$table) {
            $this->CI->load->model($table.'_model', $table);
            $this->$alias = $this->CI->$table; //给表别名赋值
        }
    }

    public function scenic($parameter=array())
    {
        if (!$this->validateChkValue($parameter)) {
            $this->setError('参数不正确！');
            return false;
        }
        $result = $this->bargaining->findByParams(array('seller_id'=>$this->sellerUid, 'goods_id'=>$this->goodsId));
        if ($result->num_rows() > 0) { /** 如果议价了。*/
            $orderProfit = $this->scenicBargainingProfit($result->row());
            $this->calculate_method = 2;
        } else { /** 走正常分润流程。*/
            $orderProfit = $this->scenicProfit();
        }
        $error = $this->getError();
        if (!empty($error)) {
            return false;
        }
        return $orderProfit;
    }
    
    /**
     * 验证参数。
     * @param unknown $parameter
     * @return boolean
     */
    private function validateChkValue($parameter)
    {
        $sysParameter = array('sellerUid','bitTrade','parent_id','cat_id','goodsId','number','bitWan');
        foreach ($sysParameter as $kv) {
            if (!isset($parameter[$kv])) {
                return false;
            }
            $this->$kv = $parameter[$kv];
        }
        $this->calculate_method = 1;
        $this->orderPayTotal = 0;
        return true;
    }
    
    /**
     * 获取订单分润表所需要的数据
     * @return void|Ambigous <void, unknown>
     */
    public function scenicProfit()
    {
        //没有议价，直接分润
        $result = $this->goods_profit->findByParams(array('status'=>'profit', 'goods_id'=>$this->goodsId, 'orderby'=>'bit_trade ASC'));
        if ($result->num_rows() <= 0) {
            $this->setError('该门票（'.$this->goodsId.'）没有设置分润。');
            return true;
        }
        return $this->getOrderProfitData($result->result());
    }
    
    public function getOrderProfitData($profitObj)
    {
        $targets = $this->getTargets($this->sellerUid);
        if (empty($targets)) {
            $this->setError('获取不到账户的上级用户');
            return false;
        }
        foreach ($profitObj as $key=>$value) {
            if ($value->bit_trade != UTID_PROVIDER /*&& $value->bit_trade != UTID_PURCHASER*/ && $value->uid == 0) { //如果用户类型不是“供货商”，也不是“采购商”
                $value->uid = $this->firstTarget($value->bit_trade, $targets);
            }
            $profitObj[$key] = $value;
        }
        return $this->orderProfitDetails($profitObj);
    }
    
    private function orderProfitDetails($profitObj)
    {
        if ($this->bitWan == 1) {//在线
            return $this->onlinePaymentProfit($profitObj);
        } else if ($this->bitWan == 2) {
            return $this->arrivalPaymentProfit($profitObj);
        }
    }
    
    /**
     * 在线支付
     * @param unknown $profitObj
     */
    private function onlinePaymentProfit($profitObj)
    {
        $orderProfit   = array();

        $salePrice     = 0; //销售价
        $ctualPay      = 0; //分润表内的实际支付价
        $reseller      = 0; //分销商利润(当前购买者)
        $beizhu16      = 0; //着想自己的利润
        $existParent   = false; //消费者账号是否存在上级用户编号一致
        foreach ($profitObj as $key=>$value) {
            $payee = array();
            //购买者区别对待。//如果用户类型不同 并且 用户uid不相等
            switch ($value->bit_trade) {
                case UTID_PROVIDER://如果是供应商，获取供应价
                    $payee['us']        = UTID_BEIZHU;
                    $payee['ut']        = $value->uid;
                    $payee['as']        = ACCOUNT_TYPE_SETTLEMENT; //1-可提现；2-冻结；4-金币；8：积份；16：月结
                    $payee['at']        = ACCOUNT_TYPE_CARRY; 
                    $payee['amount']    = $value->amount*$this->number;
                    $payee['step']      = 2; //1交易被创建 2交易被支付 3交易被消费 4交易被取消 5交易完成
                    $payee['bit_trade'] = $value->bit_trade;
                    break;
                case UTID_PROSECOND: //二级供应商
                case UTID_PURCHASER: //采购商
                    $payee['us']        = UTID_BEIZHU;
                    $payee['ut']        = $value->uid;
                    $payee['as']        = ACCOUNT_TYPE_SETTLEMENT; //1-可提现；2-冻结；4-金币；8：积份；16：月结
                    $payee['at']        = ACCOUNT_TYPE_CARRY;
                    $payee['amount']    = $value->amount*$this->number;
                    $payee['step']      = 2;
                    $payee['bit_trade'] = $value->bit_trade;
                    break;
                case UTID_BEIZHU:    //着想自己
                    $beizhu16 += $value->amount*$this->number;
                    $payee['us']        = UTID_BEIZHU;
                    $payee['ut']        = $value->uid;
                    $payee['as']        = ACCOUNT_TYPE_SETTLEMENT; //1-可提现；2-冻结；4-金币；8：积份；16：月结
                    $payee['at']        = ACCOUNT_TYPE_CARRY;
                    $payee['amount']    = $value->amount*$this->number;
                    $payee['step']      = 2;
                    $payee['bit_trade'] = $value->bit_trade;
                    break;
                case UTID_MANAGER_L1://如果是购买者是分销商，也可能是发展商，也可能是一级或二级代理
                case UTID_MANAGER_L2:
                case UTID_GROWTH:
                case UTID_SELLER:
                    if ((int)$this->bitTrade&$value->bit_trade) { //减去当前购买者的利润
                        $reseller += $value->amount*$this->number;
                    } else if ($value->uid != $this->sellerUid) {
                        $payee['us']        = UTID_BEIZHU;
                        $payee['ut']        = $value->uid;
                        $payee['as']        = ACCOUNT_TYPE_SETTLEMENT;
                        $payee['at']        = ACCOUNT_TYPE_CARRY;
                        $payee['amount']    = $value->amount*$this->number;
                        $payee['step']      = 2;
                        $payee['bit_trade'] = $value->bit_trade;
                    }
                    break;
                case UTID_ACTUAL : //8192交易者 - 实际支付金额
                    $ctualPay = $value->amount*$this->number;
                    $payee['us']        = $this->sellerUid;
                    $payee['ut']        = UTID_BEIZHU;
                    $payee['as']        = ACCOUNT_TYPE_CARRY;
                    $payee['at']        = ACCOUNT_TYPE_SETTLEMENT;
                    $payee['amount']    = $ctualPay;
                    $payee['step']      = 2;
                    $payee['bit_trade'] = $value->bit_trade;
                    break;
                case UTID_INTEGRAL : //32768 积分奖励
                    $payee['us']        = 0;
                    $payee['ut']        = $this->sellerUid;
                    $payee['as']        = 0;
                    $payee['at']        = ACCOUNT_TYPE_RANK;
                    $payee['amount']    = $value->amount*$this->number;
                    $payee['step']      = 2;
                    $payee['bit_trade'] = $value->bit_trade;
                    break;
                case UTID_BONUSES : //134217728 金币交易
                    $payee['us']        = $this->sellerUid;
                    $payee['ut']        = 0;
                    $payee['as']        = ACCOUNT_TYPE_BONUS;
                    $payee['at']        = 0;
                    $payee['amount']    = $value->amount*$this->number;
                    $payee['step']      = 2;
                    $payee['bit_trade'] = $value->bit_trade;
                    break;
                default: 
                    $this->setError('用户（'.$value->uid.'）的类型（'.$value->bit_trade.'）有误');
                    break;
            }
            if (!in_array($value->bit_trade, array(UTID_ACTUAL, UTID_INTEGRAL, UTID_BONUSES))) {
                $salePrice += $value->amount*$this->number;
            }
            if (!empty($payee)) {
                $orderProfit[] = $payee;
            }
            if (($this->bitTrade&UTID_CUSTOMER) && $this->parent_id == $value->uid) { //上级用户是分销商
                $existParent = true;
            }
        }
        if (bccomp($ctualPay, $salePrice, 2)) {
            $this->setError('分润或产品设置有误。');
            return false;
        }
        
        $userUid = ($this->bitTrade & UTID_CUSTOMER) ? $this->parent_id : $this->sellerUid;
        $result1 = $this->user_account->findByUid($userUid);
        if ($result1->num_rows() <= 0) {
            $this->setError('账户表获取有误。');
            return false;
        }
        $accountOverBeizhu = $result1->row(0);
        $overBeizhu = $beizhu16*($accountOverBeizhu->over_beizhu/100); //溢价的金额
        $beizhu16 = $beizhu16 - $overBeizhu; //着想最终的利润
        
        if ($existParent) {
            $this->orderPayTotal = $salePrice;
        } else {
            $reseller = $reseller + $overBeizhu; //分销商最终的优惠
            $this->orderPayTotal = $salePrice - $reseller; //当前实际应支付金额 - 这个分销商所分的钱
        }
        
        if (!empty($orderProfit)) { //修改分润表实际支付金额
            foreach ($orderProfit as $kk=>$vv) {
                if ($vv['ut'] == $this->parent_id && $this->parent_id != UTID_BEIZHU && $existParent) {
                    $vv['amount'] = $overBeizhu+$vv['amount'];
                    $existParent = false;//只执行一次，下次循环不走此程序。
                }
                if ($vv['bit_trade'] == UTID_ACTUAL) {
                    $vv['amount'] = $this->orderPayTotal;
                }
                if ($vv['bit_trade'] == UTID_BEIZHU) { //着想溢价所减去的金额
                    $vv['amount'] = $beizhu16;
                }
                $orderProfit[$kk] = $vv;
            }
        }
        return $orderProfit;
    }
    
    /*
     * 到付 -- 供应商 要给我们钱
     */
    private function arrivalPaymentProfit($profitObj, $product)
    {
        $orderProfit   = array();
        $payee = array();
        $salePrice     = 0; //销售价总价
        foreach ($profitObj as $key=>$value) {
            //购买者区别对待。
            switch ($value->bit_trade) {
                case UTID_PROVIDER://如果是供应商，获取供应价
                    $payee['account'] = $product->shop_price - $product->supply_price;
                    $payee['as']      = 2;
                    break;
                case UTID_PROSECOND: //二级供应商
                case UTID_PURCHASER: //采购商
                case UTID_BEIZHU:    //着想自己
                case UTID_MANAGER_L1://如果是购买者是分销商，也可能是发展商，也可能是一级或二级代理
                case UTID_MANAGER_L2:
                case UTID_GROWTH:
                case UTID_SELLER:
                    $payee['account'] = $value->amount*$this->number;
                    $payee['as']      = 1;
                    $salePrice += $value->amount*$this->number;
                    break;
                default:
                    $this->setError('用户('.$value->uid.')的类型（'.$value->bit_trade.'）有误');
                    break;
            }
            $payee['bit_trade']    = $value->type_id;
            $payee['account_type'] = ACCOUNT_TYPE_MONTH;
            $payee['uid']          = $value->uid;

            if(!empty($payee)){
                array_push($orderProfit, $payee);
            }
        }
        //到付，供应商要返给我们利润钱
        if (bccomp(($product->shop_price - $product->supply_price)*$this->number, $salePrice)){
            $this->setError('分润或产品设置有误。');
            return false;
        }
        $this->orderPayTotal = $salePrice; //当前实际应支付金额 - 这个分销商所分的钱
        return $orderProfit;
    }
    
    /**
     * 景区议价
     * @param unknown $bargainingRow
     * @return Ambigous <boolean, number, multitype:number>|Ambigous <Ambigous, number>
     */
    public function scenicBargainingProfit($bargainingRow)
    {
        $provider_id = $bargainingRow->provider_id;
        $seller_id = $bargainingRow->seller_id;
        $goods_id = $bargainingRow->goods_id;
        $shop_price = $bargainingRow->market_price;   //销售价
        $supply_price = $bargainingRow->provide_price; //供应价
        $scen_price = $bargainingRow->scen_price; //到付
        $seller_income = $bargainingRow->seller_income;
        $poundage = $bargainingRow->poundage;
        $node = $bargainingRow->node;
        $beizhu_id = $bargainingRow->beizhu_id;
        $beizhu_profit = $bargainingRow->beizhu_profit;
        $purchaser_id = $bargainingRow->purchaser_id;
        $purchaser_profit = $bargainingRow->purchaser_profit;
        $city_id = $bargainingRow->city_id;
        $city_profit = $bargainingRow->city_profit;
        $prosecond_id = $bargainingRow->prosecond_id;
        $prosecond_profit = $bargainingRow->prosecond_profit;
        $manager_l1_id = $bargainingRow->manager_l1_id;
        $manager_l1_profit = $bargainingRow->manager_l1_profit;
        $manager_l2_id = $bargainingRow->manager_l2_id;
        $manager_l2_profit = $bargainingRow->manager_l2_profit;
        $source = $bargainingRow->source;
        $bankpay = $bargainingRow->bankpay;
        $pid = $bargainingRow->pid;
        
        if ($poundage != (string)($beizhu_profit+$purchaser_profit+$city_profit+$prosecond_profit+$manager_l1_profit+$manager_l2_profit)) {
            $this->setError('产品价格设置有误，不可购买');
            return false;
        }
        
        //设置门票价格
        $this->shop_price = $bargainingRow->market_price;
        $this->supply_price = $bargainingRow->provide_price;
        
        //判断结算方式 $node 1: 自主结算; 2:系统结算
        $profitArr = array(); //填充分润数据
            
        //如果是议价联票
        if (in_array($this->cat_id, array(2, 8))) {
            $result = $this->goods_profit->findByParams(array('status'=>'profit', 'goods_id'=>$this->goodsId, 'bit_trade'=>UTID_PROVIDER, 'orderby'=>'bit_trade ASC'));
            if ($result->num_rows() <= 0) {
                $this->setError('该门票（'.$this->goodsId.'）分润设置有误。');
                return true;
            }
            $providerPrice = 0; //供应价
            foreach ($result->result() as $item) {
                $providerPrice = bcadd($providerPrice, $item->amount, 2);
                $profitArr[$item->uid]['goods_id']  = $goods_id;
                $profitArr[$item->uid]['uid']       = $item->uid;
                $profitArr[$item->uid]['amount']    = ($node == 1) ? 0 : $item->amount; //自主结算供应价为零
                $profitArr[$item->uid]['bit_trade'] = UTID_PROVIDER;
                $profitArr[$item->uid]['status']    = 'bargaining';
            }
            if ($node == 2) { //如果为系统结算，该项需要验证
                if ($providerPrice != $supply_price) {
                    $this->setError('该门票（'.$this->goodsId.'）供应总价与议价供应价不相等。');
                    return true;
                }
            }
        } else {
            if ($provider_id) { //供应商的价格
                $profitArr[$provider_id]['goods_id']  = $goods_id;
                $profitArr[$provider_id]['uid']       = $provider_id;
                $profitArr[$provider_id]['amount']    = ($node == 1) ? 0 : $supply_price; //自主结算供应价为零;
                $profitArr[$provider_id]['bit_trade'] = UTID_PROVIDER;
                $profitArr[$provider_id]['status']    = 'bargaining';
            }
        }
        
        if ($this->sellerUid) { //实际支付价格
            $profitArr[$this->sellerUid]['goods_id']  = $goods_id;
            $profitArr[$this->sellerUid]['uid']       = $this->sellerUid;
            $profitArr[$this->sellerUid]['amount']    = ($node == 1) ? $poundage : $supply_price+$poundage;; //自主结算供应价为零;
            $profitArr[$this->sellerUid]['bit_trade'] = UTID_ACTUAL;
            $profitArr[$this->sellerUid]['status']    = 'bargaining';
        }
        
        //其它的加起来属于总的手续费
        if ($purchaser_id) {
            $profitArr[$purchaser_id]['goods_id']  = $goods_id;
            $profitArr[$purchaser_id]['uid']       = $purchaser_id;
            $profitArr[$purchaser_id]['amount']    = $purchaser_profit;
            $profitArr[$purchaser_id]['bit_trade'] = 0;
            $profitArr[$purchaser_id]['status']    = 'bargaining';
        }
        if ($city_id) {
            $profitArr[$city_id]['goods_id']  = $goods_id;
            $profitArr[$city_id]['uid']       = $city_id;
            $profitArr[$city_id]['amount']    = $city_profit;
            $profitArr[$city_id]['bit_trade'] = 0;
            $profitArr[$city_id]['status']    = 'bargaining';
        }
        if ($beizhu_id) {
            $profitArr[$beizhu_id]['goods_id']  = $goods_id;
            $profitArr[$beizhu_id]['uid']       = $beizhu_id;
            $profitArr[$beizhu_id]['amount']    = $beizhu_profit;
            $profitArr[$beizhu_id]['bit_trade'] = UTID_BEIZHU;
            $profitArr[$beizhu_id]['status']    = 'bargaining';
        }
        if ($prosecond_id) {
            $profitArr[$prosecond_id]['goods_id']  = $goods_id;
            $profitArr[$prosecond_id]['uid']       = $prosecond_id;
            $profitArr[$prosecond_id]['amount']    = $prosecond_profit;
            $profitArr[$prosecond_id]['bit_trade'] = 0;
            $profitArr[$prosecond_id]['status']    = 'bargaining';
        }
        if ($manager_l1_id) {
            $profitArr[$manager_l1_id]['goods_id']  = $goods_id;
            $profitArr[$manager_l1_id]['uid']       = $manager_l1_id;
            $profitArr[$manager_l1_id]['amount']    = $manager_l1_profit;
            $profitArr[$manager_l1_id]['bit_trade'] = 0;
            $profitArr[$manager_l1_id]['status']    = 'bargaining';
        }
        if ($manager_l2_id) {
            $profitArr[$manager_l2_id]['goods_id']  = $goods_id;
            $profitArr[$manager_l2_id]['uid']       = $manager_l2_id;
            $profitArr[$manager_l2_id]['amount']    = $manager_l2_profit;
            $profitArr[$manager_l2_id]['bit_trade'] = 0;
            $profitArr[$manager_l2_id]['status']    = 'bargaining';
        }
        if (empty($profitArr)) {
            $this->setError('议价用户不存在');
            return false;
        }
        foreach ($profitArr as $uid => $item) {
            if ($item['bit_trade'] == 0) {
                $profitArr[$uid]['bit_trade'] = $this->getUserType($uid);
            }
        }
        $bargainingProfit = json_decode(json_encode($profitArr)); //将数组转化成对象
        
        if ($this->bitWan == 1) {//在线
            return $this->bargainingPaymentProfit($bargainingRow, $bargainingProfit);
        } elseif ($this->bitWan == 2) {
            return $this->arrivalPaymentBargaining($bargainingRow, $bargainingProfit);
        }
    }
    
    /**
     * 议价在线支付。
     * @param unknown $profitObj
     */
    private function bargainingPaymentProfit($bargainingRow, $bargainingProfit)
    {
        $orderProfit   = array();

        $salePrice     = 0; //总的价格
        $ctualPay      = 0; //分润表内的实际支付价
        $poundage      = 0; //总手续费
        foreach ($bargainingProfit as $key=>$value) {
            $payee = array();
            //购买者区别对待。//如果用户类型不同 并且 用户uid不相等
            switch ($value->bit_trade) {
                case UTID_PROVIDER://如果是供应商，获取供应价
                    $payee['us']        = UTID_BEIZHU;
                    $payee['ut']        = $value->uid;
                    $payee['as']        = ACCOUNT_TYPE_SETTLEMENT; //1-可提现；2-冻结；4-金币；8：积份；16：月结
                    $payee['at']        = ACCOUNT_TYPE_CARRY; 
                    $payee['amount']    = $value->amount*$this->number;
                    $payee['step']      = 2; //1交易被创建 2交易被支付 3交易被消费 4交易被取消 5交易完成
                    $payee['bit_trade'] = $value->bit_trade;
                    break;
                case UTID_PROSECOND: //二级供应商
                case UTID_PURCHASER: //采购商
                case UTID_BEIZHU:    //着想自己
                case UTID_MANAGER_L1://如果是购买者是分销商，也可能是发展商，也可能是一级或二级代理
                case UTID_MANAGER_L2:
                case UTID_GROWTH:
                case UTID_SELLER:
                    $poundage = bcadd($poundage, bcmul($value->amount, $this->number, 2), 2); //所应该付的总手续费
                    $payee['us']        = UTID_BEIZHU;
                    $payee['ut']        = $value->uid;
                    $payee['as']        = ACCOUNT_TYPE_SETTLEMENT;
                    $payee['at']        = ACCOUNT_TYPE_CARRY;
                    $payee['amount']    = $value->amount*$this->number;
                    $payee['step']      = 2;
                    $payee['bit_trade'] = $value->bit_trade;
                    break;
                case UTID_ACTUAL : //8192交易者 - 实际支付金额
                    $ctualPay = $value->amount*$this->number;
                    $payee['us']        = $this->sellerUid;
                    $payee['ut']        = UTID_BEIZHU;
                    $payee['as']        = ACCOUNT_TYPE_CARRY;
                    $payee['at']        = ACCOUNT_TYPE_SETTLEMENT;
                    $payee['amount']    = $ctualPay;
                    $payee['step']      = 2;
                    $payee['bit_trade'] = $value->bit_trade;
                    break;
                default: 
                    break;
            }
            if (!in_array($value->bit_trade, array(UTID_ACTUAL))) {
                $salePrice = bcadd($salePrice, bcmul($value->amount, $this->number, 2), 2);
            }
            if (!empty($payee)) {
                $orderProfit[] = $payee;
            }
        }
        if (bccomp($ctualPay, $salePrice, 2) != 0) {
            $this->setError('sorry，产品价格设置有误。');
            return false;
        }
        if ($bargainingRow->pid == 1 || $bargainingRow->pid == 3) { //1:购买者付 2：供应商付 3：二级供应商
            $this->orderPayTotal += $salePrice; //当前实际应支付金额 
        } else {
            $this->orderPayTotal += ($salePrice - $poundage); //当前实际应支付金额 - 总手续费
        }
        
        if (!empty($orderProfit)) { //修改分润表实际支付金额
            foreach ($orderProfit as $kk=>$vv) {
                if ($vv['bit_trade'] == UTID_ACTUAL) {
                    $vv['amount'] = $this->orderPayTotal;
                }
                $orderProfit[$kk] = $vv;
            }
        }
        return $orderProfit;
    }
    
    private function onlinePaymentBargaining($bargainingRow, $bargainingProfit)
    {
        $orderProfit = $this->getOrderProfitOnlinePayer($bargainingRow, $bargainingProfit);
        if ($bargainingRow->node == '1') { //系统结算
            switch ($bargainingRow->payer) {
                case '1' : //分销商付手续费
                    $amountTotal   = ($bargainingRow->supply_price + $bargainingRow->poundage + $bargainingRow->prosecond_profit)*$this->number;
                    break;
                case '2' : //供应商付手续费
                    $amountTotal   = ($bargainingRow->supply_price + $bargainingRow->prosecond_profit)*$this->number;
                    $orderProfit[$bargainingRow->provider_uid]['account'] = $orderProfit[$bargainingRow->provider_uid]['account'] - $bargainingRow->poundage * $this->number;
                    break;
                case '3' ://二级供应商付手续费
                    $amountTotal   = ($bargainingRow->supply_price + $bargainingRow->prosecond_profit)*$this->number;
                    $orderProfit[$bargainingRow->prosecond_id]['account'] = $orderProfit[$bargainingRow->prosecond_id]['account'] - $bargainingRow->poundage * $this->number;
                    break;
            }
            $this->orderPayTotal = $amountTotal + ($amountTotal * ($bargainingRow->bankpay/100));
        } else /*if ($bargainingRow->node == '2')*/ { //自主结算
            $this->orderPayTotal = $bargainingRow->poundage * $this->number;
            if(empty($orderProfit)){
                return true;
            }
        }
        return $orderProfit;
    }
    
    /**
     * 议价在线支付-减去指定的手续费
     * @param unknown $bargainingRow
     * @param unknown $payerUid
     * @param unknown $type_id
     * @return multitype:number string unknown NULL
     */
    private function getOrderProfitOnlinePayer($bargainingRow, $bargainingProfit)
    {
        if ($bargainingRow->node == 1){ //系统结算
            $orderProfit[$bargainingRow->provider_uid]['uid']          = $bargainingRow->provider_uid;
            $orderProfit[$bargainingRow->provider_uid]['account']      = $bargainingRow->supply_price * $this->number;
            $orderProfit[$bargainingRow->provider_uid]['account_type'] = ACCOUNT_TYPE_CARRY;
            $orderProfit[$bargainingRow->provider_uid]['bit_trade']    = UTID_PROVIDER;
            $orderProfit[$bargainingRow->provider_uid]['as']           = 1;
        }
        //二级供应商
        if ($bargainingRow->prosecond_profit > 0 && $bargainingRow->prosecond_id){
            $orderProfit[$bargainingRow->prosecond_id]['uid']          = $bargainingRow->prosecond_id;
            $orderProfit[$bargainingRow->prosecond_id]['account']      = $bargainingRow->prosecond_profit * $this->number;
            $orderProfit[$bargainingRow->prosecond_id]['account_type'] = ACCOUNT_TYPE_CARRY;
            $orderProfit[$bargainingRow->prosecond_id]['bit_trade']    = UTID_PROSECOND;
            $orderProfit[$bargainingRow->prosecond_id]['as']           = 1;
        }
        if(!empty($bargainingProfit)){
            foreach ($bargainingProfit as $key=>$value) {
                $orderProfit[$value->uid]['uid']          = $value->uid;
                $orderProfit[$value->uid]['account']      = $value->amount * $this->number;
                $orderProfit[$value->uid]['account_type'] = ACCOUNT_TYPE_CARRY;
                $orderProfit[$value->uid]['bit_trade']    = $value->type_id;
                $orderProfit[$value->uid]['as']           = 1;
            }
        }
        return $orderProfit;
    }
    
    /**
     * 到付，不存在自主结算和系统结算。
     * 到付，我们就只收手续费分润钱，不存在收取银行利息
     * @param unknown $bargainingRow
     * @param unknown $bargainingProfit
     * @return Ambigous <number, multitype:multitype:number Ambigous <multitype:number, multitype:number string unknown NULL > >
     */
    private function arrivalPaymentBargaining($bargainingRow, $bargainingProfit)
    {
        $orderProfit = $this->providerPayTotalArrivalPayer($bargainingRow, $bargainingProfit);

        $poundage = $bargainingRow->poundage *  $this->number;
        switch ($bargainingRow->payer) {
            case '1' : //分销商付手续费,到付给供应商，还是供应商欠我们手续费的钱
                $orderProfit[$this->sellerUid]['account'] = $orderProfit[$this->sellerUid]['account'] - $poundage;
                break;
            case '2' : //供应商付手续费，是供应商付我们的钱
                $orderProfit[$bargainingRow->provider_uid]['account'] = ($bargainingRow->shop_price - $bargainingRow->supply_price) * $this->number - $poundage;
                break;
            case '3' ://二级供应商付手续费，是二级供应商欠我们钱，但是二级供应商的账户余额必须要大于这个钱。
                $orderProfit[$bargainingRow->prosecond_id]['account'] = $bargainingRow->prosecond_profit * $this->number - $poundage;
                break;
        }
        //这里是分销商(当前用户)要付给供应商多少钱？。
//        $this->orderPayTotal = $bargainingRow->poundage * $this->number;
        return $orderProfit;
    }
    
    /**
     * 供应商要给我们多少钱？
     * @param unknown $bargainingRow
     */
    public function providerPayTotalArrivalPayer($bargainingRow, $bargainingProfit)
    {
        $orderProfit[$bargainingRow->provider_uid]['uid']          = $bargainingRow->provider_uid;
        $orderProfit[$bargainingRow->provider_uid]['account']      = ($bargainingRow->shop_price - $bargainingRow->supply_price) * $this->number;
        $orderProfit[$bargainingRow->provider_uid]['account_type'] = ACCOUNT_TYPE_MONTH;
        $orderProfit[$bargainingRow->provider_uid]['bit_trade']    = UTID_PROVIDER;
        $orderProfit[$bargainingRow->provider_uid]['as']           = 2;
        //二级供应商
        if ($bargainingRow->prosecond_profit > 0 && $bargainingRow->prosecond_id) {
            $orderProfit[$bargainingRow->prosecond_id]['uid']          = $bargainingRow->prosecond_id;
            $orderProfit[$bargainingRow->prosecond_id]['account']      = $bargainingRow->prosecond_profit * $this->number;
            $orderProfit[$bargainingRow->prosecond_id]['account_type'] = ACCOUNT_TYPE_MONTH;
            $orderProfit[$bargainingRow->prosecond_id]['bit_trade']    = UTID_PROSECOND;
            $orderProfit[$bargainingRow->prosecond_id]['as']           = 1;
        }
        
      if (!empty($bargainingProfit)) {
            foreach ($bargainingProfit as $key=>$value) {
                $orderProfit[$value->uid]['uid']          = $value->uid;
                $orderProfit[$value->uid]['account']      = $value->amount * $this->number;
                $orderProfit[$value->uid]['account_type'] = ACCOUNT_TYPE_MONTH;
                $orderProfit[$value->uid]['bit_trade']    = $value->type_id;
                $orderProfit[$value->uid]['as']           = 1;
            }
        }
    
        $orderProfit[$this->sellerUid]['uid']          = $this->sellerUid;
        $orderProfit[$this->sellerUid]['account']      = ($bargainingRow->shop_price - $bargainingRow->prosecond_profit - $bargainingRow->supply_price)*$this->number;
        $orderProfit[$this->sellerUid]['account_type'] = ACCOUNT_TYPE_MONTH;
        $orderProfit[$this->sellerUid]['bit_trade']    = 4096;
        $orderProfit[$this->sellerUid]['as']           = 1;
        return $orderProfit;
    }
}